package leetcode;

/**
 * https://leetcode-cn.com/problems/hamming-distance/description/
 * https://leetcode.com/problems/hamming-distance/?tab=Description
 * @program: brushing
 * @description:
 * @author: LiuQiang
 * @create: 2018-07-25 10:36
 **/
public class HammingDistance {
    public int hammingDistance(int x, int y) {
		/*int r = 0;
		if(x > y)
		{
			r = Math.abs(~y|x);
		}
		else
		{
			r = Math.abs(~x|y);
		}

		String s = String.valueOf(r);
		if(s.length() > 1)
		{
			s = s.substring(0, s.length() - 1);
		}
		return Integer.valueOf(s).intValue();*/

        String a1;
        String b1;
        if (x > y) {
            a1 = Integer.toBinaryString(y);
            b1 = Integer.toBinaryString(x);
        } else {
            a1 = Integer.toBinaryString(x);
            b1 = Integer.toBinaryString(y);
        }

        String aNums[] = a1.split("");
        String bNums[] = b1.split("");

        int aMax = a1.length();
        int bMax = b1.length();

        int flag = 0;

        if (aMax > bMax) {
            String c[] = new String[aMax];
            for (int i = 0; i < aMax - bMax; i++) {
                c[i] = "0";
            }

            for (int i = aMax - bMax; i < aMax; i++) {
                c[i] = bNums[i - (aMax - bMax)];
            }

            for (int i = 0; i < aMax; i++) {
                if (!aNums[i].equals(c[i])) {
                    flag++;
                }
            }
        } else if (bMax > aMax) {
            String c[] = new String[bMax];
            for (int i = 0; i < bMax - aMax; i++) {
                c[i] = "0";
            }

            for (int i = bMax - aMax; i < bMax; i++) {
                c[i] = aNums[i - (bMax - aMax)];
            }

            for (int i = 0; i < bMax; i++) {
                if (!bNums[i].equals(c[i])) {
                    flag++;
                }
            }
        } else {
            for (int i = 0; i < bMax; i++) {
                if (!bNums[i].equals(aNums[i])) {
                    flag++;
                }
            }
        }
        return flag;
    }

    public static void main(String[] args) {

        HammingDistance h = new HammingDistance();
        System.out.println(h.hammingDistance(93, 73));


		/*int a = 1;
		int b = 4;

		String a1 = Integer.toBinaryString(a);
		String b1 = Integer.toBinaryString(b);

		int aMax = a1.length();
		int bMax = b1.length();

		String az [] = a1.split("");
		String bz [] = b1.split("");

System.out.println("a:"+a1);
System.out.println("b:"+b1);
System.out.println(aMax +","+ bMax);

		int flag = 0;

		if(aMax >= bMax)
		{
			for (int i = 0; i < aMax; i++)
			{
				if(aMax - bMax <= i)
				{
					for (int j = 0; j < bMax; j++)
					{
						if(az[i] != bz[j])
						{
							flag++;
						}
					}
				}
				else
				{
					if(!"0".equals(az[i]))
					{
						flag++;
					}
				}
			}
		}
		else
		{
			for (int i = 0; i < bMax; i++)
			{
				if(bMax - aMax <= i)
				{
					for (int j = 0; j < aMax; j++)
					{
						if(bz[i] != az[j])
						{
							flag++;
						}
					}
				}
				else
				{
					if(!"0".equals(bz[i]))
					{
						flag++;
					}
				}
			}
		}

		System.out.println(flag);*/

/*		int c = ~a|b;
		c = Math.abs(~a|b);
		String s = String.valueOf(c);
		System.out.println(s);
		if(s.length() > 1)
		{
			System.out.println("~~" + s.substring(0,s.length()-1));
		}*/
/*		System.out.println(a&b);
		System.out.println(a|b);
		System.out.println(a^b);
		System.out.println("");
		System.out.println(~a&b);
		System.out.println(~a|b);
		System.out.println(~a^b);*/
    }
}
